﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Implementation
{
    using System.Windows.Forms;
    using ConnectBasic.Shell.Extensibility;
    using System;

    internal partial class ToolWindowZone : UserControl
    {
        private IShell mShell;
        private IToolWindowZoneCollection mZones;
        private IToolWindowZone mZone;
        private string mZoneInvariantName;

        public ToolWindowZone()
        {
            InitializeComponent();
        }

        public void Initialize(IShell shell, IToolWindowZoneCollection zones, string invariantName)
        {
            mShell = shell;
            mZones = zones;
            mZoneInvariantName = invariantName;
            mZone = zones[invariantName];            

            // Make sure we know when the shell state updates
            shell.States.ActiveStateChanged += new System.EventHandler(States_ActiveStateChanged);

            // Initialize the UI
            this.UpdateWindows();
        }

        void States_ActiveStateChanged(object sender, System.EventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new EventHandler(States_ActiveStateChanged), this, e);
            }
            else
            {
                this.UpdateWindows();
            }
        }

        private void UpdateWindows()
        {            
            var windows = mZones.GetAttachedWindows(mZone, mShell.States.ActiveState);

            WindowsTabControl.SuspendLayout();
            WindowsTabControl.TabPages.Clear();

            foreach (var window in windows)
            {

                // TODO: Not sure I can avoid this coupling between the window and the hosting zone
                var native = (DefaultToolWindow)window;
                var control = native.GetOrCreateControl(mShell);

                // Create the tab for this control
                CreateControlTab(window, control);
            }

            WindowsTabControl.ResumeLayout();
        }

        private void CreateControlTab(IToolWindow window, Control control)
        {
            TabPage page = new TabPage(window.DisplayName);
            page.Tag = window;
            page.Controls.Add(control);
            control.Dock = DockStyle.Fill;
            WindowsTabControl.TabPages.Add(page);
        }
    }
}
